﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.Plat.CompanyChargingMng.Interfaces;
using Microhard.Plat.CompanyChargingMng.DataAccess;
using Microhard.Plat.CompanyChargingMng.Msg;
using Microhard.Plat.CompanyChargingMng.Models;
using Microhard.Plat.CompanyChargingMng.DTO;


namespace Microhard.Plat.CompanyChargingMng.Impls
{
    public class EmployeeImpl : IEmployee
    {
        public Microhard.Core.DBUtility.ExecutedResult UpdateUserWeixinOpenID(string userName, string weixinOpenID)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_ChargingEntities db = new Microhard_ChargingEntities();
            var entity = db.Employees.SingleOrDefault(l => l.UserName == userName);
            if (entity != null)
            {
                //清除之前的微信ID
                var toClears = db.Employees.Where(l => l.WeixinOpenID == weixinOpenID).ToList();
                if (toClears != null)
                {
                    toClears.ForEach(t =>
                    {
                        t.WeixinOpenID = null;
                    });
                }
                entity.WeixinOpenID = weixinOpenID;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, EmployeeMsg.UpdateFailed);
                Log.Error("IEmployee.UpdateUserWeixinOpenID", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Add(Models.EmployeeModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            Microhard_ChargingEntities db = new Microhard_ChargingEntities();
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;

            try
            {
                db.Employees.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, EmployeeMsg.AddFailed);
                Log.Error("IEmployee.Add", ex);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.EmployeeModel model)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_ChargingEntities db = new Microhard_ChargingEntities();
            var entity = db.Employees.SingleOrDefault(l => l.EmployeeID == model.EmployeeID);
            if (entity != null)
            {
                entity.Name = model.Name;
                entity.Cellphone = model.Cellphone;
                entity.QQMail = model.QQMail;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, EmployeeMsg.UpdateFailed);
                Log.Error("IEmployee.Update", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_ChargingEntities db = new Microhard_ChargingEntities();
            var toDelete = db.Employees.Where(e => ids.Contains(e.EmployeeID)).ToList();
            toDelete.ForEach(e =>
            {
                //使用删除
                db.Employees.DeleteObject(e);
            });
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, EmployeeMsg.DeleteFailed);
                    Log.Error("IEmployee.Delete", ex);
                }
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.EmployeeModel model)
        {
            throw new NotImplementedException();
        }

        public Models.EmployeeModel GetModelByID(Guid id)
        {
            Microhard_ChargingEntities db = new Microhard_ChargingEntities();
            var entity = db.Employees.SingleOrDefault(e => e.EmployeeID == id);
            if (entity != null)
            {
                return entity.ToModel();
            }
            return null;
        }

        public Models.EmployeeModel GetModelByModel(Models.EmployeeModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.EmployeeModel> Query(Models.EmployeeModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            Microhard_ChargingEntities db = new Microhard_ChargingEntities();
            IEnumerable<Employee> entities = from b in db.Employees
                                             where this.scope.PKID.Contains(b.EmployeeID)
                                             select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.Name).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<EmployeeModel>();
        }

        public List<Models.EmployeeModel> Query(Models.EmployeeModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }


        public CompanyModel GetUserCompanyByUserName(string userName)
        {
            Microhard_ChargingEntities db = new Microhard_ChargingEntities();
            var cmp = db.Employees.SingleOrDefault(u => u.UserName == userName);
            if (cmp != null)
            {
                return cmp.Company.ToModel();
            }
            return null;
        }


        public bool IsValidUser(string userName)
        {
            bool result = true;
            Microhard_ChargingEntities db = new Microhard_ChargingEntities();
            try
            {
                var emp = db.Employees.Single(u => u.UserName == userName);
                if (emp.Company == null)
                {
                    result = false;
                }
            }
            catch (Exception ex)
            {
                result = false;
            }
            return result;
        }


        public bool SystemAccountIsUsing(string accountName)
        {
            Microhard_ChargingEntities db = new Microhard_ChargingEntities();
            var cnt = db.Employees.Count(e => e.UserName == accountName);
            return cnt > 0 ? true : false;
        }


        public bool HasBindWeixinOpenID(string weixinOpenID)
        {
            Microhard_ChargingEntities db = new Microhard_ChargingEntities();
            var cnt = db.Employees.Count(e => e.WeixinOpenID == weixinOpenID);
            return cnt > 0 ? true : false;
        }


        public EmployeeModel GetByUserName(string userName)
        {
            Microhard_ChargingEntities db = new Microhard_ChargingEntities();
            var entity = db.Employees.SingleOrDefault(e => e.UserName == userName);
            if (entity != null)
            {
                return entity.ToModel();
            }
            return null;
        }


    }
}
